<?php

/**
 * 报名|支付|邀请 统计 每天凌晨1点运行
 *
 * @author zhoujf
 *
 */
require_once dirname(__DIR__) . "/cronBase.php";

use Common\Bridge;

class enroll extends cronBase
{
    public function doDefault()
    {
        global $argv;
        //计算昨天的时间
        $time = !empty($argv[1]) && !IS_ONLINE ? strtotime($argv[1]) : time() - 86400;
        $yesterday = date('Y-m-d', $time);
        $yesterday1 = date('Y-m-d 00:00:00', $time);
        $yesterday2 = date('Y-m-d 23:59:59', $time);
        $types = loadconf('statistics/type');

        //用户报名人数
        $type_enroll = $types['enroll'];
        $select_sql = "SELECT COUNT(*) AS `num` FROM EnrollUser WHERE EnrollTime >= '" . $yesterday1 . "' and EnrollTime <= '" . $yesterday2 . "'";
        $enroll_num = $this->userQuery($select_sql);
        $rows = ['Type' => $type_enroll, 'Num' => intval($enroll_num[0]['num']), 'CreateDate' => $yesterday];
        //写入数据
        $pdo = Bridge::pdo(Bridge::DB_ADMIN_W);
        $pdo->insert('SiteStatus', $rows, ['ignore' => true]);

        //用户邀请人数
        $type_invite = $types['invite'];
        $select_sql = "SELECT COUNT(*) AS `num` FROM UserRelationship WHERE CreateTime >= '" . $yesterday1 . "' and CreateTime <= '" . $yesterday2 . "'";
        $invite_num = $this->userQuery($select_sql);
        $rows = ['Type' => $type_invite, 'Num' => intval($invite_num[0]['num']), 'CreateDate' => $yesterday];
        //写入数据
        $pdo = Bridge::pdo(Bridge::DB_ADMIN_W);
        $pdo->insert('SiteStatus', $rows, ['ignore' => true]);

        //用户支付人数
        $type_pay = $types['notice'];
        $select_sql = "SELECT COUNT(*) AS `num` FROM Payment WHERE CreateTime >= '" . $yesterday1 . "' and CreateTime <= '" . $yesterday2 . "' AND Status IN (2,5)";
        $pay_num = $this->centerQuery($select_sql);
        $rows = ['Type' => $type_pay, 'Num' => intval($pay_num[0]['num']), 'CreateDate' => $yesterday];
        //写入数据
        $pdo = Bridge::pdo(Bridge::DB_ADMIN_W);
        $pdo->insert('SiteStatus', $rows, ['ignore' => true]);

    }

    /**
     * 查询语句
     *
     * @param string $sql 查询的SQL语句
     * @return array
     */
    private function userQuery($sql)
    {
        $pdo = Bridge::pdo(Bridge::DB_USER_R);
        $obj = $pdo->query($sql);

        return $obj->fetchAll();
    }

    /**
     * 查询语句
     *
     * @param string $sql 查询的SQL语句
     * @return array
     */
    private function centerQuery($sql)
    {
        $pdo = Bridge::pdo(Bridge::DB_CENTER_R);
        $obj = $pdo->query($sql);

        return $obj->fetchAll();
    }
}

$app->run();